package com.etime.perfectschool.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.perfectschool.mapper.FoodCommentsMapper;
import com.etime.perfectschool.mapper.FoodPhotoMapper;
import com.etime.perfectschool.mapper.SchoolFoodMapper;
import com.etime.perfectschool.pojo.*;
import com.etime.perfectschool.service.SchoolFoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SchoolFoodServiceImpl implements SchoolFoodService {

    @Autowired
    private SchoolFoodMapper schoolFoodMapper;
    @Autowired
    private FoodPhotoMapper foodPhotoMapper;
    @Autowired
    private FoodCommentsMapper foodCommentsMapper;


    @Override
    public Page<SchoolFood> getAllSchoolFood(int current,int size) {
        Page<SchoolFood> page = new Page<>(current,size);
        return schoolFoodMapper.getAllSchoolFood(page);
    }

    @Override
    public int addSchoolFood(SchoolFood schoolFood) {
        schoolFoodMapper.addSchoolFood(schoolFood);
        int foodId = schoolFood.getFoodId();
        return foodId;

    }

    @Override
    public boolean deleteSchoolFood(int foodId) {
        boolean res=false;
        List<FoodComments> allFoodComments = foodCommentsMapper.getAllFoodComments(foodId);
        List<FoodPhoto> allFoodPhotos = foodPhotoMapper.getAllFoodPhotoByfoodId(foodId);
        if(allFoodComments!=null)
        {
            foodCommentsMapper.deleteFoodCommentsByfoodId(foodId);
            if(allFoodPhotos!=null)
            {
                foodPhotoMapper.deleteFoodPhotoById(foodId);
            }
        }else {
            if(allFoodPhotos!=null)
            {
                foodPhotoMapper.deleteFoodPhotoById(foodId);
            }
        }
        res= schoolFoodMapper.deleteById(foodId)==0?false:true;
        return res;
    }

    @Override
    public boolean likeSchoolFood(SchoolFood schoolFood) {
        return schoolFoodMapper.updateById(schoolFood)==0?false:true;
    }

    @Override
    public SchoolFood getSchoolFood(int foodId) {
        return schoolFoodMapper.selectById(foodId);
    }

    @Override
    public boolean updateSchoolFood(SchoolFood schoolFood) {
        return schoolFoodMapper.updateById(schoolFood)==0?false:true;
    }
}
